package com.tzxs.pay.main.bill.source;

import android.app.Application;
import android.util.Log;

import com.tzxs.pay.main.bill.detail.model.MsgRefundBean;
import com.tzxs.pay.main.bill.detail.model.RefundBean;
import com.tzxs.pay.main.bill.detail.model.VerifyOrderBean;
import com.tzxs.pay.main.bill.model.AutBeanDetail;
import com.tzxs.pay.main.bill.model.AutOrderBean;
import com.tzxs.pay.main.bill.model.CoInDetailBean;
import com.tzxs.pay.main.bill.model.CollectionBillEntity;
import com.tzxs.pay.main.bill.model.GtBean;
import com.tzxs.pay.main.deposit.model.SettlementBean;
import com.tzxs.pay.main.gather.model.ScanAutBean;
import com.tzxs.pay.main.gather.model.ScanPayBean;
import com.tzxs.pay.main.onlineaut.model.OnlineAutBean;
import com.tzxs.pay.main.onlineaut.model.OnlineAutPrint;
import com.tzxs.pay.main.onlineaut.model.OnlineAutSureInBean;
import com.tzxs.pay.main.onlineaut.model.OnlineDetailBean;
import com.tzxs.pay.main.onlineaut.model.ReSendMsgBean;
import com.tzxs.pay.util.ISharedPreference;
import com.tzxs.pay.util.RetrofitUtil;
import com.tzxs.pay.util.RxUtil;
import com.tzxs.pay.util.netnavigator.NetNavigator;

import java.lang.ref.WeakReference;

public class BillDataSource {

    private static BillDataSource IINSTANCE;


    private WeakReference<Application> app;
    //获取本地存储类对象
    private ISharedPreference sharedPreference;

    private BillDataSource(WeakReference<Application> app) {
        this.app = app;
        sharedPreference = ISharedPreference.getInstance(app.get());
    }


    //获取单例
    public static BillDataSource getINSTANCE(Application application) {
        if (IINSTANCE == null) {
            synchronized (BillDataSource.class) {
                if (IINSTANCE == null) {
                    IINSTANCE = new BillDataSource(new WeakReference<>(application));
                }
            }
        }
        return IINSTANCE;
    }


    /**
     * 获取收款订单
     *
     * @param navigator    回调
     * @param type         类型
     * @param page         页数
     * @param number       数量
     * @param out_trade_no 订单号
     * @param trade_no     系统订单号
     * @param time_start   开始的时间
     * @param time_end     结束的时间
     */
    public void getBills(NetNavigator<CollectionBillEntity> navigator, String type, String payType, int page, int number, String out_trade_no,
                         String trade_no, String time_start, String time_end) {

        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.getCollBillList(sharedPreference.getToken(),
                type, payType, page, number, out_trade_no, trade_no, time_start, time_end), navigator);
    }


    /**
     * 获取到单个订单详情(普通收款)
     */
    public void getBillDetail(NetNavigator<CoInDetailBean> navigator, String tradeNum) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.getCollBillDeail(sharedPreference.getToken(), tradeNum), navigator);
    }


    /**
     * 获取押金统计订单  可以按条件筛选 和预授权订单一个接口
     * 押金统计其实就是预授权列表获取
     *
     * @param type       查询类型
     * @param page       页数
     * @param pageNum    每页数量
     * @param outOrderNo 订单号
     * @param remarks    备注
     * @param timeStart  开始时间
     * @param timeEnd    结束时间
     * @param payStatus  支付状态
     * @param typeTime   按照时间查询  本月 本周等。
     * @param navigator  回调
     */
    public void getDepositBills(String type, int page, int pageNum, String outOrderNo,
                                String remarks, String timeStart, String timeEnd, int payStatus, String typeTime, NetNavigator<AutOrderBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.getDepositBills(sharedPreference.getToken(), type,
                page,
                pageNum,
                outOrderNo,
                remarks,
                timeStart,
                timeEnd,
                payStatus,
                typeTime), navigator);

    }


    /**
     * 获取搜索收款  分期账单列表
     *
     * @param type      类型
     * @param note      备注
     * @param startTime 开始时间
     * @param endTime   结束时间
     */
    public void getCollectionResultBills(String type, String payType, String note, int time_type, int page, int num, String outOrderNo,
                                         String startTime, String endTime, NetNavigator<CollectionBillEntity> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService
                .getSearchCollAndInstallList(sharedPreference.getToken(),
                        type,
                        payType,
                        page,
                        num,
                        note,
                        time_type,
                        outOrderNo,
                        startTime,
                        endTime), navigator);
    }


    /**
     * @param aut_no  授权号
     * @param pay_num 消费金额
     * @param remarks 备注
     */
    public void normalSettlement(String aut_no, String pay_num, String remarks, NetNavigator<SettlementBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService
                .normalSettlement(sharedPreference.getToken(),
                        aut_no,
                        pay_num,
                        remarks), navigator);
    }


    /**
     * 二维码收款
     *
     * @param remarks   备注
     * @param code      二维码扫描结果
     * @param amount    金额
     * @param desc      描述
     * @param navigator na
     */
    public void scanPay(String remarks, String code, String amount, String desc, NetNavigator<ScanPayBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService
                .scanPay(sharedPreference.getToken(), remarks, code, amount, desc), navigator);

    }

    /**
     * 二维码预授权
     *
     * @param remarks   备注
     * @param code      二维码扫描结果
     * @param amount    金额
     * @param navigator na
     */
    public void scanAutPay(String remarks, String code, String amount, NetNavigator<ScanAutBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService
                .scanAutPay(sharedPreference.getToken(), remarks, code, amount), navigator);

    }

    /**
     * 获取单个预授权订单详情
     *
     * @param orderId   订单号
     * @param navigator 回调
     */
    public void getAutBillDetail(String orderId, NetNavigator<AutBeanDetail> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.getAutDetail(sharedPreference.getToken(), orderId), navigator);
    }

    /**
     * 退款
     *
     * @param orderId   订单号
     * @param bz        备注
     * @param password  支付密码
     * @param amount    金额
     * @param navigator 回调
     */

    public void refund(String orderId, String bz, String password, String amount, NetNavigator<RefundBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.refund(sharedPreference.getToken(), orderId, bz, password, amount), navigator);
    }


    /**
     * 获取首页统计昨天的数据
     *
     * @param navigator 回调
     */
    public void getFirstPageData(NetNavigator<CollectionBillEntity> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.getFirstPageStatistical(sharedPreference.getToken(), 2), navigator);
    }


    /**
     * 获取在线预授权列表
     *
     * @param page      1
     * @param num       10
     * @param navigator 回调
     */
    public void getOnlineAutList(int page, int num, NetNavigator<OnlineAutBean> navigator) {

        Log.d("测试", "getOnlineAutList:  page: " + page + "num:====" + num + "token:====" + sharedPreference.getToken());
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.onlineAutList(sharedPreference.getToken(), page, num), navigator);
    }

    /**
     * 在线预授权item详情
     *
     * @param orderId     订单id
     * @param printStatus 打印状态
     * @param navigator   回调
     */
    public void getOnlineAutDetail(String orderId, String printStatus, NetNavigator<OnlineDetailBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.onlineAutDetail(sharedPreference.getToken(), orderId, printStatus), navigator);
    }

    /**
     * 在线预授权item详情打印
     *
     * @param orderId     订单id
     * @param printStatus 打印状态
     * @param navigator   回调
     */
    public void getOnlineAutPrint(String orderId, String printStatus, NetNavigator<OnlineAutPrint> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.onlineAutPrint(sharedPreference.getToken(), orderId, printStatus), navigator);
    }


    /**
     * 创建在线预授权订单
     *
     * @param price        金额
     * @param bookingName  入住人名
     * @param bookingPhone 入住人手机号
     * @param receivePhone 短信接收手机号
     * @param bookingTime  入住时间
     * @param remarks      备注
     * @param navigator    navigator
     */
    public void createMsgAut(float price, String bookingName, String bookingPhone, String receivePhone,
                             String bookingTime, String remarks, NetNavigator<ReSendMsgBean> navigator) {

        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.createMsgAutOrder(sharedPreference.getToken(),
                price, bookingName, bookingPhone, receivePhone, bookingTime, remarks), navigator);
    }


    //重新发送短信
    public void resendMsg(String orderId, NetNavigator<ReSendMsgBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.resendMsg(sharedPreference.getToken(), orderId), navigator);
    }

    /**
     * 短信预授权核销
     *
     * @param orderId   订单id
     * @param navigator navigator
     */
    public void cancelMsgAut(String orderId, NetNavigator<OnlineAutSureInBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.orderCancel(sharedPreference.getToken(), orderId), navigator);
    }

    //获取在线预授权搜索结果列表
    public void getSearchAutsList(String traceNo, String orderId, String bookingName,
                                  String bookingPhone, String orderPhone, String startTime, String endTime,
                                  NetNavigator<OnlineAutBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.searchAutsList(sharedPreference.getToken(), traceNo, orderId
                , bookingName, bookingPhone, orderPhone, startTime, endTime), navigator);
    }

    //开始推送消息
    public void startGt(String cid, String title, String content, NetNavigator<GtBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.startGt(sharedPreference.getToken(), cid, title, content), navigator);
    }


    //超过时间短信验证码退款
    public void msgRefund(String payPwd, String verifystr, String orderId, String bz, NetNavigator<MsgRefundBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.startMsgRefund(sharedPreference.getToken(), payPwd, verifystr, orderId, bz), navigator);
    }

    //验证订单是否超过退款期限
    public void verifyOrder(String orderId, NetNavigator<VerifyOrderBean> navigator) {
        RxUtil.ioMain(RetrofitUtil.getInstance().apiService.verifyOrder(sharedPreference.getToken(), orderId), navigator);
    }
}
